using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace CheckConnection
{
    public partial class Form1 : Form
    {
        CheckConnection ckCnn;

        public Form1()
        {
            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            checkBt.Enabled = false;
        }

        private void connectBt_Click(object sender, EventArgs e)
        {
            ckCnn = new CheckConnection();
            ckCnn.ServerName = serverNameTB.Text;

            if (backgroundWorker1.IsBusy != true)
                backgroundWorker1.RunWorkerAsync();
        }

        private void checkBt_Click(object sender, EventArgs e)
        {
            ckCnn.Database = databaseNameTB.Text;

            if (backgroundWorker2.IsBusy != true)
                backgroundWorker2.RunWorkerAsync();
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            ckCnn.CheckCnn();
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (ckCnn.ResultServer == true)
            {
                connectResultTB.Text = "Successful";
                checkBt.Enabled = true;
            }
            else
            {
                connectResultTB.Text = "Fail";
                checkBt.Enabled = false;
            }
        }

        private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
        {
            ckCnn.checkDatabase();
        }

        private void backgroundWorker2_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (ckCnn.ResultDB == true)
            {
                checkResultTB.Text = String.Format("{0} database is exists", ckCnn.Database);
            }
            else
                checkResultTB.Text = String.Format("{0} database is not exists", ckCnn.Database);
        }
    }
}
